/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// shell_parser.cpp - shell command pareser, refer "shell_parser_fsm.vsd"


#include "..\global.h"	
#include "shell.h"	



static bool IsSpace(char ch) 
{
	return (ch==' ' || ch=='\t' || ch=='\r');
}


static bool IsTokenChar(char ch)
{
	char chars[] = "+-.:_";
	// "+" is used for key_down commands
	// "-" is used for key_up commands
	// "." is used for floating point numbers
	// ":" can be used for name-spaces
	// "_" is commonly used character
	
	if ('a'<=ch && ch<='z') return true;
	if ('A'<=ch && ch<='Z') return true;
	if ('0'<=ch && ch<='9') return true;
	
	for (char *scan=chars; *scan; scan++) {
		if (ch==*scan) return true;
	}
	
	return false;
}


static bool IsStringChar(char ch)
{
	char chars[] = " \t`~!@#$%^&*()_+-=\\|[]{};':<>,./?";
	
	if (IsTokenChar(ch)) return true;

	for (char *scan=chars; *scan; scan++) {
		if (ch==*scan) return true;
	}
	
	return false;
}


/*-----------------------------------------------------------------------------
	Parsing stuff :
-----------------------------------------------------------------------------*/

//
//	EShell::ParseText
//
void EShell::ParseText(const char *text)
{
	string token;
	
	const char *scan = text;

	parsed_command.clear();

	while (*scan) {
		//	Skip space :
		while (IsSpace(*scan)) {
			scan++;
		}

		//	Skip comments :
		if (ParseComment(scan)) continue;

		//	End of the command (new line or ';') :
		if (*scan=='\n' || *scan==';') {
			CallCommand();
			parsed_command.clear();
			*scan++;
			continue;
		}
		
		//	Parse strings and tokens :
		if (ParseString(scan))  continue;
		if (ParseToken(scan))   continue;

		//	Unexpected character :		
		if (isprint(*scan)) {
			RAISE_EXCEPTION(va("Unexpected character 0x%02X '%c'", *scan, *scan));
		} else {
			RAISE_EXCEPTION(va("Unexpected character 0x%02X", *scan));
		}
	}	
}


//
//	EShell::ParseComment
//
bool EShell::ParseComment(const char *&scan)
{
	if ( !(scan[0]=='/' && scan[1]=='/') ) {
		return false;
	}

	*scan++; 
	*scan++;
	
	while (*scan) {
		if (*scan=='\n') {
			*scan++;
			break;
		}
		*scan++;
	}
	
	return true;
}


//
//	EShell::ParseString
//
bool EShell::ParseString(const char *&scan)
{
	if (scan[0]!='"') {
		return false;
	}
	
	scan++;

	parsed_token.clear();
	
	while (IsStringChar(*scan)) {
		parsed_token.push_back(*scan);
		scan++;
	}
	
	if (*scan!='"') {
		RAISE_EXCEPTION("unmatched quotes");
	}

	scan++;

	parsed_command.push_back( parsed_token );
	return true;
}


//
//	EShell::ParseToken
//
bool EShell::ParseToken(const char *&scan)
{
	if (!IsTokenChar(*scan)) {
		return false;
	}
	
	parsed_token.clear();

	while (IsTokenChar(*scan)) {
		parsed_token.push_back(*scan);
		scan++;
	}
	
	parsed_command.push_back( parsed_token );
	return true;
}